#Gets all newfeeds from google rss feed and puts them into newsfeeds table
#also selects newsfeds from database to show in bets [age in "news" section 
require 'rss'
class NewsfeedsController < ApplicationController
  # List feeds by team_id
  # Save and render a couple of news
  def index
   @sid = params[:sid].to_i
   unless @sid>0
     return 
   end
   match = Match.find_by_id(@sid)
   @team = []
   @feeds = []
   
   @team[1] = match.away_team_id if match.away_team_id 
   @team[0] = match.home_team_id if match.home_team_id

   @team.each do |t|
    
     last = Newsfeed.find(:first, :order=>'pubdate desc', :conditions=>["team_id = ?", t])
     if last
       last_publish = last.created_at
     end
     
     puts last_publish
     if !last_publish
       last_publish = 1.month.ago
     end
    
     
     if (last_publish.to_time < (Time.now - 2.hour).to_time)
       self.grab_newsfeed(t, last_publish)
     end
     feed = Newsfeed.find(:all, :order=>'pubdate desc', :conditions=>["team_id = ?", t], :limit=>2)
   
     @feeds << feed
end


    render :partial=>'index',:locals=>{:sid=>@sid,:user=>@user}
 end
 
 
 
  def grab_newsfeed(t, last)
   team = Team.find_by_id(t)
   if team
     rss_url = "http://news.google.com/news"
     team_name = team.display_name ? team.display_name : team.name
     q = team_name+'+'+team.sport.name+'+team'
     q = q.downcase.gsub(" ", "+" )
    
     params = {:pz=>'1', :ned=>'us', :hl=>'en', :q=>q, :output=>'rss', :scoring=>'d', :num=>'20'}
     full_addr = rss_url+"?"+params.to_query
    # puts Rails.logger.debug full_addr
     rss = RSS::Parser.parse(open(full_addr).read, false)
     items = rss.items.reverse
     items.each do |item|
    
      if item.pubDate.to_time>last.to_time
        nf = Newsfeed.new
        nf.team_id = t
        nf.title = item.title if item.title
        nf.description = item.description if item.description
        nf.pubdate = item.pubDate.to_s if item.pubDate
        nf.link = item.link if item.link
        nf.save
     
       end
     end
    
     return full_addr
   else
     return false
   end
 end
  
  
  def get_pare_feeds
    
    feed_id =  params[:sid].to_i
  
    @team_id = Newsfeed.find_by_id(feed_id).team_id.to_i
   
    @match_id = params[:match_id].to_i if params[:match_id]
    
    puts "team_id"+@team_id.to_s
    puts "match_id"+@match_id.to_s
    puts "sum"+(@team_id+@match_id).to_s
    @feeds = []
    type= params[:dop] if params[:dop]
    case type
      when "next"
        conditions = ["team_id=? and id < ?",  @team_id, feed_id]
        limit=3
        order = 'pubdate desc'
      else
        conditions = ["team_id=? and id > ?",  @team_id, feed_id]
        # last 3-rd record don't use
        limit=3
        order = 'pubdate asc'
    end
      
      @feeds= Newsfeed.find(:all, :order=>order, :conditions=>conditions, :limit=>limit)
      if type=='prev'
        @feeds = @feeds.reverse
      end

      if @feeds.count==0 
        point = feed_id.to_s 
      else
        point = @feeds[0].id.to_s
      end
      @next_count = Newsfeed.count :id, :conditions=>" id < "+point+" and team_id="+@team_id.to_s
      @prev_count = Newsfeed.count :id, :conditions=>" id > "+point+" and team_id="+@team_id.to_s
   
      render :partial=>'pare_feeds',:locals=>{:sid=>@sid,:user=>@user}
    
  end
 
 
  
end
